package com.zhihesj.anan.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhihesj.anan.message.Message;
import com.zhihesj.anan.system.entity.Menu;
import com.zhihesj.anan.system.entity.RoleMenu;
import com.zhihesj.anan.system.service.MenuService;
import com.zhihesj.anan.system.service.RoleMenuService;
import com.zhihesj.anan.utils.HutoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色和菜单关联表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/system/role-menu")
public class RoleMenuController {

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private MenuService menuService;

    /**
     * 根据前端传回角色ID，菜单,修改数据库sys_role_menu
     * @param map1
     * @return
     */
    @Transactional
    @PostMapping("/updateRoleToMenu")
    public Message updateRoleToMenu(@RequestBody Map<String, Object> map1){
        HutoolUtil hutoolUtils = new HutoolUtil();
//      获取参数
        ArrayList<Object> arrayList = (ArrayList<Object>) map1.get("data");
        Long roleId = ((Integer) map1.get("roleId")).longValue();
        List<RoleMenu> roleMenuList = new ArrayList<>();
        RoleMenu roleMenu;
        Long menuId;
        Long parentId;
        List<Long> menuList = new ArrayList<>();
        List<Long> parentList = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            Map<String, Object> map = (Map<String, Object>) arrayList.get(i);
            menuId = ((Integer) map.get("menuId")).longValue();
            menuList.add(menuId);
            parentId=((Integer) map.get("parentId")).longValue();
            if (parentId > 0L) {
                parentList.add(parentId);
            }
        }
//        查询出全部菜单ID
        ergodicMenuId(menuList, parentList);
        menuList=hutoolUtils.mergeList(menuList);

        for (Long aLong : menuList) {
            roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(aLong);
            roleMenuList.add(roleMenu);
        }
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleMenuService.remove(wrapper);
        roleMenuService.saveBatch(roleMenuList);

        List<Menu> list=menuService.selectRoleKey();
        for (Menu menu : list) {
            menuService.updateById(menu);
        }
        return Message.success();
    }

    /**
     * 遍历全部菜单
     * @param list 回传list
     * @param parentIdList 父菜单ID
     */
    private void ergodicMenuId(List<Long> list,List<Long> parentIdList){
        List<Menu> menuList = new ArrayList<>();
        List<Long> listLong = new ArrayList<>();
        if (parentIdList.size() > 0) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("menu_id",parentIdList);
            menuList=menuService.list(queryWrapper);
        }
        for (Menu menu : menuList) {
            list.add(menu.getMenuId());
            if (menu.getParentId() > 0L) {
                listLong.add(menu.getParentId());
            }
        }
        if (listLong.size() > 0) {
            ergodicMenuId(list, listLong);
        }
    }


}
